//
//  RightMenuViewController.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/6.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "RightMenuViewController.h"
#import "LeftMenuViewController.h"
#import "HomePageViewController.h"
#import "RegisterViewController.h"
#import "ShoppingCarViewController.h"
#import "WishListViewController.h"
#import "OrderViewController.h"
#import "CurrencyConversionViewController.h"
#import "ContactUsViewController.h"
#import "JPUSHService.h"
#import "ReturnViewController.h"
#import "PricvacyViewController.h"
#import "ShippingViewController.h"

@interface RightMenuViewController ()<UIGestureRecognizerDelegate,LeftMenuViewControllerDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSArray *allDatas;

@property(nonatomic,strong)UILabel * ShoppingBagCountLabel;
@property(nonatomic,strong)UILabel * WishListCountLabel;
@property(nonatomic,strong)UILabel * ODCountLabel;


@property(nonatomic,assign)NSInteger SBCount;
@property(nonatomic,assign)NSInteger WLCount;
@property(nonatomic,assign)NSInteger ODCount;

@end

@implementation RightMenuViewController

-(UITableView *)tableView{
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    [JPUSHService setBadge:0];


    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.frame =CGRectMake( ACTUAL_WIDTH(100), ACTUAL_WIDTH(20), KScreenWidth-ACTUAL_WIDTH(100), KScreenHeight-ACTUAL_WIDTH(20));
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor=[UIColor whiteColor];
    }
    return _tableView;
}


-(UILabel *)ShoppingBagCountLabel{
    
    if(!_ShoppingBagCountLabel){
        _ShoppingBagCountLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(230), ACTUAL_HEIGHT(20), ACTUAL_WIDTH(25), ACTUAL_HEIGHT(25))];
        _ShoppingBagCountLabel.backgroundColor=[UIColor blackColor];
        _ShoppingBagCountLabel.textColor=[UIColor whiteColor];
        _ShoppingBagCountLabel.layer.cornerRadius = 12.5;
        _ShoppingBagCountLabel.layer.masksToBounds = YES;
        _ShoppingBagCountLabel.textAlignment=1;
        [_ShoppingBagCountLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
    }
    return _ShoppingBagCountLabel;
}

-(UILabel *)WishListCountLabel{
    if(!_WishListCountLabel){
        _WishListCountLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(230), ACTUAL_HEIGHT(20), ACTUAL_WIDTH(25), ACTUAL_HEIGHT(25))];
        _WishListCountLabel.backgroundColor=[UIColor blackColor];
        _WishListCountLabel.textColor=[UIColor whiteColor];
        _WishListCountLabel.layer.cornerRadius = 12.5;
        _WishListCountLabel.layer.masksToBounds = YES;
        _WishListCountLabel.textAlignment=1;
        [_WishListCountLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
    }
    return _WishListCountLabel;
}

-(UILabel *)ODCountLabel{
    if(!_ODCountLabel){
        _ODCountLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(230), ACTUAL_HEIGHT(20), ACTUAL_WIDTH(25), ACTUAL_HEIGHT(25))];
        _ODCountLabel.backgroundColor=[UIColor blackColor];
        _ODCountLabel.textColor=[UIColor whiteColor];
        _ODCountLabel.layer.cornerRadius = 12.5;
        _ODCountLabel.layer.masksToBounds = YES;
        _ODCountLabel.textAlignment=1;
        [_ODCountLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
    }
    return _ODCountLabel;
}




#pragma mark - viewDidLoad
- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.tableView];

}

- (void)viewWillAppear:(BOOL)animated
{
    if([UserSession instance].cookie != nil){
        [self getCountDatas];
    }
    [_tableView reloadData];
}

#pragma mark - getDatas
-(void)getCountDatas{
    
        //shopping_cart
        //http://viprunningonline.com/api/index.php?main_page=shopping_cart
        NSString*urlStr1=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
        NSDictionary*params1=@{@"main_page":@"shopping_cart",
                               @"zendid":[UserSession instance].cookie};
        HttpManager*manager1=[[HttpManager alloc]init];
        [manager1 getDataFromNetworkWithUrl:urlStr1 parameters:params1 compliation:^(id data, NSError *error) {
            
            NSString * number=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            if ([number isEqualToString:@"1"]) {
                self.SBCount=[data[@"data"][@"cart"] count];
            }
            [self.tableView reloadData];
        }];
        
        
        //my_wishlist
        //http://viprunningonline.com/api/index.php?main_page=my_wishlist
        NSString*urlStrr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
        NSDictionary*paramss=@{@"main_page":@"my_wishlist",
                               @"zendid":[UserSession instance].cookie};
        HttpManager*managerr=[[HttpManager alloc]init];
        [managerr getDataFromNetworkWithUrl:urlStrr parameters:paramss compliation:^(id data, NSError *error) {
            
            NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            if ([number isEqualToString:@"1"]) {
                self.WLCount=[data[@"data"] count];
            }
            [self.tableView reloadData];
        }];
    
    
        // http://viprunningonline.com/api/index.php?main_page=account_history
        
//        NSString *urlString=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
//        NSDictionary * paramss3=@{@"main_page":@"account_history",
//                               @"zenid":[UserSession instance].cookie,};
    NSString*urlString=[NSString stringWithFormat:@"%@main_page=account_history",HTTP_ADDRESS];
    NSDictionary*paramss3=@{ @"zenid":[UserSession instance].cookie,
                            @"current_page_number":@"1"};
        HttpManager * managerr3=[[HttpManager alloc]init];
        
        [managerr3 postDataFromNetworkWithUrl:urlString parameters:paramss3 compliation:^(id data, NSError *error) {
            
            //NSLog(@"%@",data);
            
            NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
            
            if ([number isEqualToString:@"1"]) {
                
                self.ODCount = [data[@"data"][@"total_records"] intValue];
                
                [self.tableView reloadData];
            }
        }];
}

#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return 9;
    return 6;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return ACTUAL_HEIGHT(130);
    }else{
        return 0.0001;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ACTUAL_HEIGHT(55);
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    NSArray*imageArray=@[@"rightbar_shopping",
                         @"rightbar_wish_list",
                         @"rightbar_shopping",
                         @"rightbar_store",
                         @"rightbar_newsletter",
//                         @"right_Return",
//                         @"right_Pricvacy",
//                         @"right_Shipping",
                         @"rightbar_cancellation"];
    
    NSArray *array = @[@"SHOPPING",
                       @"WISH LIST",
                       @"ORDER",
                       @"CURRENCY",
                       @"CONTACT",
//                       @"RETURN POLICY",
//                       @"PRICVACY POLICY",
//                       @"SHIPPING POLICY",
                       @"CANCELLATION"];
    
    UIImageView *logoImageView=[[UIImageView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(25), ACTUAL_HEIGHT(20), ACTUAL_WIDTH(21), ACTUAL_HEIGHT(21))];
    [logoImageView setImage:[UIImage imageNamed:imageArray[indexPath.row]]];
    [cell addSubview:logoImageView];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(70), ACTUAL_HEIGHT(20), ACTUAL_WIDTH(200), ACTUAL_HEIGHT(25))];
    titleLabel.text=array[indexPath.row];
    titleLabel.font = FONT_SYSTEM_28;
    titleLabel.textColor = RGBACOLOR(51, 51, 51,1);
    [cell addSubview:titleLabel];
    
    
    if (indexPath.row==0) {
            
       if (self.SBCount > 0) {
           self.ShoppingBagCountLabel.text=[NSString stringWithFormat:@"%ld",(long)self.SBCount];
           [cell addSubview:self.ShoppingBagCountLabel];
            
        }else{
            [self.ShoppingBagCountLabel removeFromSuperview];
        }
    }
        
    if (indexPath.row==1){
        
        if (self.WLCount > 0) {
            self.WishListCountLabel.text=[NSString stringWithFormat:@"%ld",(long)self.WLCount];
            [cell addSubview:self.WishListCountLabel];
        }else{
            [self.WishListCountLabel removeFromSuperview];
        }
    }
    
    if(indexPath.row == 2){
        if (self.ODCount > 0) {
            self.ODCountLabel.text=[NSString stringWithFormat:@"%ld",(long)self.ODCount];
            [cell addSubview:self.ODCountLabel];
        }else{
            [self.ODCountLabel removeFromSuperview];
        }
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if(indexPath.row == 0){
        ShoppingCarViewController *vc = [[ShoppingCarViewController alloc] init];
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
        return;
    }

    if(indexPath.row == 1){
        WishListViewController * vc = [[WishListViewController alloc] init];
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
        return;
    }
    
    if(indexPath.row == 2){
        //order
        OrderViewController * vc = [[OrderViewController alloc] init];
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
        return;
    }
    
    if(indexPath.row == 3){
         CurrencyConversionViewController * vc= [[CurrencyConversionViewController alloc] init];
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
        return;
    }
    
    if(indexPath.row == 4){
        ContactUsViewController * vc = [[ContactUsViewController alloc] init];
        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
        return;
    }
    
//    if(indexPath.row == 5){
//        PrivacyStatementViewController * vc = [[PrivacyStatementViewController alloc] init];
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{}];
//        return;
//    }
//    if (indexPath.row == 5) {
//      
//        ReturnViewController * returnVC = [[ReturnViewController alloc]init];
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:returnVC  withCompletion:^{}];
//        return;
//    }
//    if (indexPath.row == 6) {
//        
//        PricvacyViewController * pricacyVC = [[PricvacyViewController alloc]init];
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:pricacyVC  withCompletion:^{}];
//        return;
//    }
//    if (indexPath.row == 7) {
//        
//        ShippingViewController * shippingVC = [[ShippingViewController alloc]init];
//        [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:shippingVC  withCompletion:^{}];
//        return;
//    }
    
    if(indexPath.row == 5){
        _SBCount = 0;
        _WLCount = 0;
        _ODCount = 0;
        if([UserSession instance].isLogin){
        
            [UserSession instance].isLogin = NO;
            [UserSession instance].cookie = @"";
            [UserSession instance].email = @"";
            [MobClick profileSignOff];
            
//            [[UserSession instance] cleanUser];
            [self reloadInputViews];
            
            [tableView reloadData];
            [JRToast showWithText:@"login out success!" duration:2.0];
            
            HomePageViewController * vc = [[HomePageViewController alloc] init];
            [[SlideNavigationController sharedInstance] popToRootAndSwitchToViewController:vc  withCompletion:^{
                NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
                NSNotification * notification = [[NSNotification alloc]initWithName:@"letHomePageVCToRefresh" object:self userInfo:nil];
                [center postNotification:notification];
            }];
            
            
            //http://viprunningonline.com/api/index.php? main_page=logoff
            NSString*urlStr=[NSString stringWithFormat:@"%@",HTTP_ADDRESS];
            NSDictionary*params=@{@"main_page":@"logoff"};
            HttpManager*manager=[[HttpManager alloc]init];
            
            [manager getDataFromNetworkWithUrl:urlStr parameters:params compliation:^(id data, NSError *error) {
                
            }];
        
        }else{
            [JRToast showWithText:@"You have not logged in！"];
        }
       
        return;
    }
    
    
    
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *BGView=[[UIView alloc]initWithFrame:CGRectMake(0, ACTUAL_HEIGHT(0), KScreenWidth-ACTUAL_WIDTH(50), ACTUAL_HEIGHT(130))];
    BGView.backgroundColor=[UIColor whiteColor];
    
    UIImageView *touxiangImageView=[[UIImageView alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(90), ACTUAL_HEIGHT(16), ACTUAL_WIDTH(70), ACTUAL_HEIGHT(70))];
    touxiangImageView.layer.cornerRadius=ACTUAL_WIDTH(35);
    [touxiangImageView setImage:[UIImage imageNamed:@"img_touxiang"]];
    [BGView addSubview:touxiangImageView];
    
    UILabel *signLabel=[[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(-35), ACTUAL_HEIGHT(90), KScreenWidth-ACTUAL_WIDTH(50), ACTUAL_HEIGHT(20))];
    signLabel.textAlignment=1;
    if ([UserSession instance].email.length==0) {
        signLabel.text=@"LOGIN/SIGN IN";
        signLabel.font = FONT_SYSTEM_28;
        signLabel.textColor = RGBACOLOR(204, 204, 204, 1);
    }else{
        signLabel.text=[UserSession instance].email;
    }
    [BGView addSubview:signLabel];
    
    UIButton *registerButton=[[UIButton alloc]initWithFrame:BGView.frame];
    [registerButton addTarget:self action:@selector(gotoRegister) forControlEvents:UIControlEventTouchUpInside];
    [BGView addSubview:registerButton];
    
    return BGView;
}


#pragma mark - button
-(void)gotoRegister{
    
    if ([UserSession instance].isLogin==YES) {
        [JRToast showWithText:@"You have landed, don't need to register！"];
        [self.tableView reloadData];
        
        //_registerButton.userInteractionEnabled=NO;
    }else{
        RegisterViewController * registerVC = [[RegisterViewController alloc] init];
        
        [[SlideNavigationController sharedInstance] switchToViewController:registerVC withCompletion:nil];
        
    }
}


#pragma mark - didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
